//#define USEWPF
using System;
using System.Collections;
using System.Linq;
using System.Windows.Controls;
using NUnit.Framework;

namespace NotifiableLinqTests.FrameWork
{
	class CollectionListenerStragegy : IListenerStragegy
	{
		public Func<object> Expector { get; set; }

		public IEnumerable Expected
		{
			get
			{
				return (IEnumerable)Expector();
			}
		}

		CollectionNotificationListener _collectionListener;
		IEnumerable _queryResult;
			
#if USEWPF
		ListBox _wpfListbox;
#endif
		public void InitializeListener(object queryResult)
		{
			_queryResult = (IEnumerable)queryResult;
			_collectionListener = new CollectionNotificationListener(_queryResult);
#if USEWPF
			_wpfListbox = new ListBox { DataContext = _queryResult };
			_wpfListbox.SetBinding(ItemsControl.ItemsSourceProperty, "");
#endif
		}

		public void PerformAssert()
		{
			var expected = Expected.Cast<object>().ToArray();
			
			var actualCurrent = _queryResult.Cast<object>().ToArray();
			CollectionAssert.AreEqual(expected, actualCurrent, "Factical");
			
			var actualAfterNotify = _collectionListener.Cast<object>().ToArray();
			CollectionAssert.AreEqual(expected, actualAfterNotify, "Notify");
#if USEWPF
			actualAfterNotify = _wpfListbox.Items.Cast<object>().ToArray();
			CollectionAssert.AreEqual(expected, actualAfterNotify);
#endif

		}
	}
}